function queryStock() {
    console.log("start invoke!");
    /**$.ajax({
        cache: true,
        url: "http://hq.sinajs.cn/list=s_sh000001,s_sz399001,s_sz399006,s_sz399005,s_sb899002",
        type: "GET",
        dataType: "script",
        success: function (data) {
            console.log(hq_str_s_sh000001);
            console.log(data);
        }
    });**/

    $.get("http://vip.stock.finance.sina.com.cn/quotes_service/api/json_v2.php/Market_Center.getHQNodeData?page=1&num=200&sort=changepercent&asc=0&node=hs_a&symbol=&_s_r_a=page", function (result) {
        console.log(result);
        savedItems =[];
        for (var i = 0; i < result.length; i++) {
            if (result[i].changepercent < 9.5) {
                continue;
            }
            if (result[i].changepercent > 11 && result[i].changepercent < 19.5) {
                continue;
            }
            if (result[i].changepercent > 21 && result[i].changepercent < 29.5) {
                continue;
            }
            if (result[i].changepercent > 31) {
                continue;
            }
            var str = "<tr>";
            str += combineTd(result[i].symbol) + combineTd(result[i].name) + combineTd(result[i].trade)
                + combineTd(result[i].changepercent) + combineTd(result[i].settlement) + combineTd(result[i].open)
                + combineTd(result[i].high) + combineTd(result[i].low) + combineTd(result[i].volume) + combineTd(result[i].amount);
            //str += "<button onclck='removeItem("+result[i].symbol+")'>去掉</button>";
            str += "</tr>"
            $("#raiseLimitTableId").append(str);
            savedItems.push(result[i]);
        }
    });
}

function saveCurrentStock(){
   window.localStorage.rlItem = JSON.stringify(savedItems);
}

function showMyStock(){
    var result = JSON.parse(window.localStorage.rlItem);
    $("#raiseLimitTableId").html("");
    for (var i = 0; i < result.length; i++) {
        var str = "<tr>";
                str += combineTd(result[i].symbol) + combineTd(result[i].name) + combineTd(result[i].trade)
                    + combineTd(result[i].changepercent) + combineTd(result[i].settlement) + combineTd(result[i].open)
                    + combineTd(result[i].high) + combineTd(result[i].low) + combineTd(result[i].volume) + combineTd(result[i].amount);
                str += "<td><button onclick='removeItem(\""+result[i].symbol+"\")'>去掉</button></td>";
                str += "</tr>"
        $("#raiseLimitTableId").append(str);
    }
}

function removeItem(symbol){
    var result = JSON.parse(window.localStorage.rlItem);
    var tempResults = [];
    for (var i = 0; i < result.length; i++) {
        if(result[i].symbol!=symbol){
            tempResults.push(result[i]);
        }
    }
    window.localStorage.rlItem = JSON.stringify(tempResults);
    showMyStock();
}

function combineTd(value) {
    return "<td>" + (!value?"":value) + "</td>";
}

function addHotCodes(){
    var code = $("#hotCodes").val()
    $.ajax({
        cache: true,
        url: "http://suggest3.sinajs.cn/suggest/type=&key="+code+"&name=suggestdata_1640510244592",
        type: "GET",
        dataType: "script",
        success: function (data) {
            if(suggestdata_1640510244592==null || suggestdata_1640510244592==''){
                alert("股票不存在");
                return;
            }
            var arr = suggestdata_1640510244592.split(",");
            var symbol = arr[0];
            var name = arr[4];
            var result = JSON.parse(window.localStorage.rlItem);
            var isInsert = true;
            for (var i = 0; i < result.length; i++) {
                if(result[i].symbol==symbol){
                    isInsert = false;
                }
            }
            if(isInsert){
                result.push({symbol:symbol,name:name});
                window.localStorage.rlItem = JSON.stringify(result);
                showMyStock();
            }
           
        }
    });
    
}

function randomSelect(){
    var result = JSON.parse(window.localStorage.rlItem);
    var codes = [];
    for(var i=0;i<result.length;i++){
        codes.push(result[i].symbol);
    }
    var returns = getRandomArrayElements(codes,$("#randomNumber").val());
    $("#randomStocks").val(returns.join(","));
}

function getRandomArrayElements(arr, count) {
    var shuffled = arr.slice(0), i = arr.length, min = i - count, temp, index;
    while (i-- > min) {
    index = Math.floor((i + 1) * Math.random());
    temp = shuffled[index];
    shuffled[index] = shuffled[i];
    shuffled[i] = temp;
    }
    return shuffled.slice(min);
}
    var items = ['1','2','4','5','6','7','8','9','10'];
    console.log( getRandomArrayElements(items, 4) );